#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define BUFFER_SIZE 8
int main(int argc , char* argv[]){
	if(argc < 2){
		perror("argc==0");
		exit(1);
	}
	ssize_t fd =open(argv[1],O_RDWR);
	if(fd==-1){
		perror("open argv[1] failure");
		exit(1);
	}
	ssize_t wfd=open(argv[2],O_RDWR | O_CREAT | O_APPEND,00700);
	if(wfd==-1){
		perror("open argv[2] failure");
	}
	char buff[128];
	while(1){
		ssize_t byte=read(fd,buff,sizeof(buff));
		if(byte==-1){
			perror("read failure");
			exit(1);
		}else if(byte==0){
			break;
		}
		ssize_t byte_write=write(wfd,buff,byte);
		if(byte_write == -1){
			perror("write failure");
			exit(1);
		}
	}
	lseek(wfd,0,SEEK_SET);
	while(1){
		ssize_t byte =read(wfd,buff,sizeof(buff));
		if(byte==-1){
			perror("write failure");
			exit(1);
		}else if(byte==0){
			break;
		}
		if(	write(0,buff,byte)== -1){
			perror("write failure");
			exit(1);
		}
	}
	close(fd);
	return 0;
}
